<?php


class AdminMenusModel extends \think\Model
{
    protected $table = 'admin_menus';
    protected $pk = 'id';

    public function children()
    {
        return $this->hasMany(self::class, 'parentid');
    }
    /**
     * 获得所有菜单
     */
    static function getMenus($parentid = 0)
    {
        $return = [];
        $menus = self::where('parentid', $parentid)->order('sort desc')->select();
        foreach ($menus as $item) {
            $_ = [
                'title' => $item->title,
                'icon' => 'fa '.$item->icon,
                'href' => '/'.trim($item->url,'/'),
                'target' => $item->target ?? '_self',
                'child' => self::getMenus($item->id)
            ];
//            if (!empty($_['child'])) $_['href'] = '';
            $return[] = $_;
        }
        return $return;
    }
}
